package cn.tedu._06notice.controller;

import cn.tedu._06notice.base.response.JsonResult;
import cn.tedu._06notice.base.response.StatusCode;
import cn.tedu._06notice.mapper.NoticeMapper;
import cn.tedu._06notice.pojo.dto.NoticeAddParam;
import cn.tedu._06notice.pojo.dto.NoticeQuery;
import cn.tedu._06notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._06notice.pojo.entity.Notice;
import cn.tedu._06notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._06notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
/*
* Slf4j注解：1.Lombok提供的注解
*           2.一旦在类上添加该注解,会为该类创建一个日志对象log;
* */
@Api(tags = "01.资讯管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;

    @ApiOperation("添加资讯")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam = " + noticeAddParam);
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);

        return JsonResult.ok();
    }

    @ApiOperation("资讯列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsonResult list(NoticeQuery noticeQuery) {
        log.debug("noticeQuery = " + noticeQuery);
        String title = noticeQuery.getTitle();
        Integer type = noticeQuery.getType();
        List<NoticeListVO> list = noticeMapper.listNotice(title, type);
        return JsonResult.ok(list);
    }

    @ApiOperation("资讯详情")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, dataType = "Long")
    @GetMapping("1")
    public JsonResult datail(Long id) {
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetail = noticeMapper.getNoticeDetail(id);
        return JsonResult.ok(noticeDetail);
    }

    @ApiOperation("删除资讯")
    @ApiOperationSupport(order = 40)
    @PostMapping("del")
    public JsonResult delete(Long id) {
        log.debug("id = " + id);
        int rows = noticeMapper.deleteNotice(id);
        return rows > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @ApiOperation("修改资讯")
    @ApiOperationSupport(order = 50)
    @PostMapping("update")
    public JsonResult upate(NoticeUpdateParam noticeUpdateParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.updateNotice(notice);
        return num > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

}
